#include "AssetManager.h"
#include "Rendering/Essentials/TextureLoader.h"
#include "Rendering/Essentials/ShaderLoader.h"
#include "Log.h"

namespace SCION_RESOURCES
{
  bool AssetManager::AddTexture(const std::string &textureName, const std::string &texturePath, bool pixelArt)
  {
    if (m_mapTextures.find(textureName) != m_mapTextures.end())
    {
      SCION_ERROR(fmt::format("Failed to add texture [{}] -- Already exists!", textureName));
      return false;
    }
    auto texture = std::move(SCION_RENDERING::TextureLoader::Creat(
        pixelArt ? SCION_RENDERING::Texture::TextureType::PIXEL : SCION_RENDERING::Texture::TextureType::BLENDED,
        texturePath));

    if (!texture)
    {
      SCION_ERROR(fmt::format("Failed to load texture [{}] at path [{}]", textureName, texturePath));
      return false;
    }
    m_mapTextures.emplace(textureName, std::move(texture));
    return true;
  }
  const SCION_RENDERING::Texture &AssetManager::GetTexture(const std::string &textureName)
  {
    auto texItr = m_mapTextures.find(textureName);
    if (texItr == m_mapTextures.end())
    {
      SCION_ERROR(fmt::format("Failed to get texture [{}] -- Does not exist!", textureName));
      return SCION_RENDERING::Texture();
    }
    return *texItr->second;
  }
  bool AssetManager::AddShader(const std::string &shaderName, const std::string &vertextPath, const std::string &fragmentPath)
  {
    if (m_mapShader.find(shaderName) != m_mapShader.end())
    {
      SCION_ERROR(fmt::format("Failed to add shader [{}] -- Already Exists!", shaderName));
      return false;
    }
    // ������������ɫ��
    auto shader = std::move(SCION_RENDERING::ShaderLoader::Create(vertextPath, fragmentPath));
    if (!shader)
    {
      SCION_ERROR(fmt::format("Failed to load Shader [{}] at vert path [{}] and frag path [{}]", shaderName, vertextPath, fragmentPath));
      return false;
    }
    m_mapShader.emplace(shaderName, std::move(shader));

    return true;
  }
  SCION_RENDERING::Shader &AssetManager::GetShader(const std::string &shaderName)
  {
    auto shaderItr = m_mapShader.find(shaderName);
    if (shaderItr == m_mapShader.end())
    {
      SCION_ERROR(fmt::format("Failed to get shader [{}] -- Does not exist!", shaderName));
      SCION_RENDERING::Shader shader{};
      return shader;
    }
    return *shaderItr->second;
  }
}